﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;

public partial class EditTopic : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string id = Request.Params["tid"];
            CmsDataContext db = new CmsDataContext();
            var q = from aT in db.Topic
                    where aT.ID == id
                    select aT;
            if (q.Count() > 0)
            {
                Topic topic = q.First();
                TopicID.Value = topic.ID;
                TopicName.Text = topic.Name;
                TopicDescription.Text = topic.Description;
                if (topic.ParentID != null)
                {
                    ParentTopicID.Value = topic.ParentID;
                    NameLabel.InnerText = topic.Parent.Name;
                }
            }
        }
    }
    protected void Save_Click(object sender, EventArgs e)
    {
        CmsDataContext db = new CmsDataContext();

        //判断栏目名称是否存在
        var q = from aT in db.Topic
                where aT.Name == TopicName.Text.Trim()
                select aT;
        if (q.Count() > 0)
        {
            Message.Message = "栏目名称已经存在。";
            Message.Location = null;
        }
        else
        {
            Topic topic;

            if (TopicID.Value == "")
            {
                //新增
                topic = new Topic();
                topic.ID = Toolkit.NextUUID();
                db.Topic.InsertOnSubmit(topic);
            }
            else
            {
                //更新
                topic = (from aT in db.Topic
                         where aT.ID == TopicID.Value
                         select aT).First();
            }

            topic.Description = TopicDescription.Text.Trim();
            topic.Name = TopicName.Text.Trim();
            topic.ParentID = ParentTopicID.Value.Trim() == "" ? null : ParentTopicID.Value.Trim();

            db.SubmitChanges();

            Message.Message = "栏目信息保存成功。";
            Message.Location = "./EditTopic.aspx?tid=" + topic.ID;
        }
    }
}